package com.yoda.youdianda.util;

import com.yoda.youdianda.domain.SysMenu;
import com.yoda.youdianda.vo.MenuDetailVo;
import org.apache.logging.log4j.util.Strings;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author shawn
 * @date 2022年 11月 12日 20:18
 * 工具类
 */

public class Util {

    /**
     * 角色id转换
     *
     * @param roleIds 角色id
     * @return {@link List}<{@link String}>
     */
    public static List<String> roleIdTransform(String roleIds) {
        if (Strings.isEmpty(roleIds)) {
            return null;
        }
        String[] strings = roleIds.substring(1, roleIds.length() - 1).split(",");
        return Arrays.stream(strings).map(s -> s.substring(1, s.length() - 1)).collect(Collectors.toList());
    }

    public static List<MenuDetailVo> getStructuredDirectory(List<SysMenu> menus, SysMenu fatherMenu){
        List<MenuDetailVo> list = new ArrayList<>();
        //过滤次级菜单
        List<SysMenu> baseMenus = menus.stream().filter(menu -> Objects.equals(menu.getPid(), fatherMenu.getId())).collect(Collectors.toList());
        if (baseMenus.size()==0){
            //退出条件,装配vo
            return null;
        }
        //根据次级菜单再次过滤
        for (SysMenu baseMenu : baseMenus) {
            List<MenuDetailVo> vos = getStructuredDirectory(menus, baseMenu);
            MenuDetailVo fatherVo = MenuDetailVo.init(baseMenu);
            fatherVo.setChildren(vos);
            list.add(fatherVo);
        }
        return list;
    }

}